package com.itheima.redbaby.engine.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.redbaby.ContantValue;
import com.itheima.redbaby.GloableParams;
import com.itheima.redbaby.bean.Address;
import com.itheima.redbaby.bean.Address_info;
import com.itheima.redbaby.bean.CheckAddUp;
import com.itheima.redbaby.bean.Checkout_addup;
import com.itheima.redbaby.bean.Invoice;
import com.itheima.redbaby.bean.Logistics;
import com.itheima.redbaby.bean.Order;
import com.itheima.redbaby.bean.Product;
import com.itheima.redbaby.engine.BaseEngine;
import com.itheima.redbaby.engine.OrderEngine;

public class OrderEngineImpl extends BaseEngine implements OrderEngine {

	// 1=>近一个月订单 2=>一个月前订单 3=>已取消订单
	public static final int THIS_MONTH = 1;
	public static final int BEFORE_MONTH = 2;
	public static final int CANCEL_ORDER = 3;

	// int type=THIS_MONTH;

	int page = 1;
	int pageNum = 100;

	String userid;
	private Map<String, Object> map;

	@Override
	public List<Order> getOrderList(int type) {
		// TODO 测试userid
		
		
		if(GloableParams.isLogin){
			userid=GloableParams.userid+"";
		}else{
			userid="1";
		}

		List<Order> orderList = null;

		map = new HashMap<String, Object>();
		map.put("type", type);
		map.put("page", page);
		map.put("pageNum", pageNum);

		JSONObject json = getJson(BaseEngine.TYPE_GET,
				ContantValue.REDBOY_ORDERLIST_URI, map, userid);

		if (json != null) {
			String response = json.getString("response");
			if (response.equals("error")) {
				// 提示用户
			} else {
				String strJson = json.getString("orderlist");
				orderList = JSON.parseArray(strJson, Order.class);
			}
		}
		return orderList;
	}

	@Override
	public boolean cancelOrder(String orderId) {
		map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		JSONObject json = getJson(BaseEngine.TYPE_POST,
				ContantValue.REDBOY_ORDERCANCEL_URI, map);
		if (json != null) {
			String response = json.getString("response");
			if (response.equals("ordercancel")) {
				return true;
			} else {
				return false;
			}
		}
		
		return false;
	}

	@Override
	public Logistics getLogistics(String orderId) {

		map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		JSONObject json = getJson(BaseEngine.TYPE_GET,ContantValue.REDBOY_LOGISTICS_URI, map);
		if(json!=null){
			String response = json.getString("response");
			if (response.equals("error")) {
				// 错误提示
			} else {
				String strJson = json.getString("logistics");
				Logistics logistice = JSON.parseObject(strJson, Logistics.class);
				return logistice;
			}
		}

		return null;
	}

	@Override
	public Map<String, Object> getOrderDetail(String orderId) {
		Map<String, Object> orderDetail=new HashMap<String,Object>();
		map = new HashMap<String, Object>();
		map.put("orderId", orderId);
		JSONObject json = getJson(BaseEngine.TYPE_GET,ContantValue.REDBOY_ORDERDETAIL_URI, map);
		if(json!=null){
			
			String response = json.getString("response");
			if (response.equals("error")) {
				// 错误提示
			} else {
				
				String strCheckJson = json.getString("checkout_addup");
				Checkout_addup checkout_addup = JSON.parseObject(strCheckJson, Checkout_addup.class);
				orderDetail.put("checkout_addup", checkout_addup);
				
				String strProJson = json.getString("productlist");
				List<Product> productList = JSON.parseArray(strProJson, Product.class );
				orderDetail.put("productList", productList);
				
				String strInvJson = json.getString("invoice_info");
				Invoice invoice = JSON.parseObject(strInvJson, Invoice.class);
				orderDetail.put("invoice", invoice);
				
				JSONObject payJson = json.getJSONObject("payment_info");
				int payment_type=payJson.getInteger("type");
				orderDetail.put("payment_type", payment_type);
				
				String strAddJson = json.getString("address_info");
				Address_info address_info = JSON.parseObject(strAddJson, Address_info.class);
				orderDetail.put("address_info", address_info);
				
				
				JSONObject delJson = json.getJSONObject("delivery_info");
				int delivery_type=delJson.getInteger("type");
				orderDetail.put("delivery_type", delivery_type);
				
				String strOrderJson = json.getString("order_info");
				Order order_info = JSON.parseObject(strOrderJson, Order.class);
				orderDetail.put("order_info", order_info);
				
				String checkout_prom = json.getString("checkout_prom");
				orderDetail.put("checkout_prom", checkout_prom);
				
			}
		}
		
		return orderDetail;
	}
	
	

}
